<?php
/**
 * @title 1487. 保证文件名唯一
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $names
     * @return String[]
     */
    function getFolderNames($names) {
        /**
         * @since 2024-01-23 文件名哈希
         */
        $nameHash = [];

        /**
         * @since 2024-01-23 新的名字
         */
        $newNameArray = [];

        /**
         * @since 2024-01-23 遍历文件名数组
         */
        foreach ($names as $name){
            /**
             * @since 2024-01-23 是否存在相同名字
             */
            if(isset($nameHash[$name])){
                /**
                 * @since 2024-01-23 文件名序号
                 */
                $fileNumber = $nameHash[$name]+1;

                /**
                 * @since 2024-01-23 新的名称
                 */
                while(true){
                    $newName = $name ."({$fileNumber})";
                    if(!isset($nameHash[$newName])){
                        $nameHash[$name] = $fileNumber;
                        break;
                    } else {
                        $fileNumber++;
                    }
                }
            } else {
                $newName = $name;
            }

            /**
             * @since 2024-01-23 记录新名称
             */
            $nameHash[$newName] = 0;
            $newNameArray[] = $newName;
        }

        /**
         * @return String[]
         */
        return $newNameArray;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.23_2.php
 */
$datas = [
    [["pes", "pes", "pes(1)", "pes", "pes(1)", "pes(1)"]], //
    [["gta","gta(1)","gta","avalon"]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->getFolderNames(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
